package com.example.villagedevelop.generator.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.villagedevelop.generator.entity.Develop;
import com.example.villagedevelop.generator.entity.DevelopVO;
import com.example.villagedevelop.generator.entity.ImageDevelop;
import com.example.villagedevelop.generator.mapper.DevelopMapper;
import com.example.villagedevelop.generator.mapper.ImageDevelopMapper;
import com.example.villagedevelop.generator.service.DevelopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.villagedevelop.generator.service.ImageDevelopService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author villagedevelop
 * @since 2021-11-13
 */
@Service
public class DevelopServiceImpl extends ServiceImpl<DevelopMapper, Develop> implements DevelopService {
    @Autowired
    DevelopMapper mapper;
    @Autowired
    ImageDevelopService service;
    @Autowired
    ImageDevelopMapper imageDevelopMapper;

    @Override
    public List<DevelopVO> findDevelopVOByPage(int current) {
        List<DevelopVO> developVOList = new ArrayList<>();
        Page<Develop> page = new Page<>(current,5);
        IPage<Develop> result = mapper.selectPage(page,null);
        List<Develop> list = result.getRecords();


        for (Develop ca : list) {
            DevelopVO developVO = new DevelopVO();
            BeanUtils.copyProperties(ca,developVO);
            int did = ca.getId();
            developVO.setImages(service.getImagesByDid(did));
            developVOList.add(developVO);
        }
        return developVOList;
    }

    @Override
    public void insertDevelopVO(DevelopVO developVO) {
        Develop develop = new Develop();
        BeanUtils.copyProperties(developVO,develop);
        mapper.insert(develop);
        ImageDevelop image = new ImageDevelop();
        int did = develop.getId();
        for (String url : developVO.getImages()){
            image.setDId(did);
            image.setImageFile(url);
            imageDevelopMapper.insert(image);
        }
    }
}
